
const requireDirectory = require('require-directory');
const Router = require('koa-router');

class Init {



    static initCore(app) {
        //入口方法
        Init.app = app;
        Init.initLoadRouters();
        Init.loadHttpException();
        Init.loadConfig();
    }

    //导入配置项
    static loadConfig(path = '') {
        const configPath = path || process.cwd() + '/config/config.js'
        const config = require(configPath)
        global.config = config
    }

    // 导入路由
    static initLoadRouters() {
        const root = `${process.cwd()}/app/api`;
        requireDirectory(module, root, { visit: whenLoadModule })
        function whenLoadModule(obj) {
            if (obj instanceof Router) {
                Init.app.use(obj.routes())
            }
        }
    }

    // 导入异常输出
    static loadHttpException() {
        const errors = require('./httpException');
        global.errs = errors
    }
}

module.exports = Init